package com.unionftech.common.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.unionftech.common.widget.RootView
import java.lang.ref.WeakReference

abstract class BaseFragment : Fragment(), View.OnClickListener {
    private lateinit var weakReference: WeakReference<FragmentActivity>
    private var lastClick: Long = 0L
    protected lateinit var mContext: Context
    protected lateinit var mActivity: FragmentActivity
    protected lateinit var mRootView: RootView
    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (!::weakReference.isInitialized) {
            weakReference = WeakReference(requireActivity())
        }
        mContext = weakReference.get()!!
        mActivity = weakReference.get()!!
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getIntentData()
        initData()
    }

    private fun getIntentData() {
        arguments?.let {
            val bundle: Bundle = it
            obtainIntentData(bundle)
        }
    }

    protected open fun obtainIntentData(bundle: Bundle) {

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (!::mRootView.isInitialized) {
            mRootView = RootView(mContext)
            inflaterContentView(inflater, container, savedInstanceState)?.let {
                mRootView.addContentView(it)
            }
        }
        val parent = mRootView.parent as? ViewGroup
        parent?.removeAllViewsInLayout()
        return mRootView
    }

    fun getRootView(): RootView {
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        initLanguageText()
        setListener()
        setView()
    }

    protected abstract fun inflaterContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View?

    protected open fun initView(view: View) {

    }

    /**
     * 初始化界面语言Text,
     * 需要将整个界面需要展示的组件文字全部重新setText一遍
     */
    protected open fun initLanguageText() {

    }

    protected open fun initData() {

    }

    protected open fun setListener() {

    }

    protected open fun setView() {

    }

    protected open fun loadData() {

    }

    fun hideBackImg() {
        mRootView.hideBackImg()
    }

    fun showBackImg() {
        mRootView.showBackImg {
            backPressed()
        }
    }

    fun setTitle(title: String) {
        mRootView.setTitleText(title)
    }

    fun hideTitle() {
        mRootView.hideTitleText()
    }

    fun showRightImg(imgRes: Int, click: View.OnClickListener) {
        mRootView.showRightImg(imgRes, click)
    }

    fun hideTitleBar() {
        mRootView.hideTitleBar()
    }


    /**
     * View点击
     */
    protected open fun doClick(v: View?) {

    }

    /**
     * 标题栏的返回按钮的默认操作，可以重写此方法，更改操作
     */
    protected open fun backPressed() {
        mActivity.onBackPressedDispatcher.onBackPressed()
    }

    /**
     * 防止用户快速点击
     */
    private fun fastClick(): Boolean {
        if (System.currentTimeMillis() - lastClick <= 500) {
            return false
        }
        lastClick = System.currentTimeMillis()
        return true
    }

    override fun onClick(v: View?) {
        if (fastClick()) {
            doClick(v)
        }
    }

    protected open fun showLoading() {
        val activity = mActivity as BaseActivity
        activity.showLoading()
    }

    protected open fun hideLoading() {
        val activity = mActivity as BaseActivity
        activity.hideLoading()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}